<?php

namespace App;

use App\Model;
use DB;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Agent extends Authenticatable
{
    protected $rememberTokenName = '';
    protected $guarded = [];
    protected $table = 'm_dailis';

    public function users()
    {
    	return $this->hasMany('\App\User', 'daili_id', 'id');
    }

    public function historyCash()
    {
    	$cash = Withdraw::select(DB::raw('SUM(money)'))->where(['agent_id' => $this->id, 'status' => 1])->value('SUM(money)');
    	return $cash ? $cash : 0;
    }

    public function parent()
    {
        return Agent::where(['id' => $this->pid])->first();
    }

    public function sonCost()
    {
        $fee = 0;
        $ids = User::where(['daili_id' => $this->id])->get()->toArray();
        if ($ids) {
            $fee = Order::select(DB::raw('SUM(order_amount)'))->where(['pay_status' => 1])->whereIn('user_id', $ids)->value('SUM(order_amount)');
            $fee = $fee ? $fee : 0;
        }
        return $fee;
    }

    public function user()
    {
        return User::where(['account' => 'wx_'.$this->unionid])->first();
    }

    /**
     * [secondNum 二级代理数量]
     * @return [type] [description]
     */
    public function secondNum()
    {
        $agent = Agent::where(['pid' => $this->id])->get();
        return count($agent);
    }

    /**
     * [userNum 玩家数量]
     * @return [type] [description]
     */
    public function userNum()
    {
        $user = User::where(['daili_id' => $this->id])->get();
        return count($user);
    }

    /**
     * [todayCash 今日佣金]
     * @return [type] [description]
     */
    public function todayCash()
    {
        $begin = date('Y-m-d', time());
        $end = date('Y-m-d', strtotime('+1 day'));
        if ($this->pid == 0) {
            $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['type' => 1, 'daili_id' => $this->id])->whereBetween('created_at', [$begin, $end])->value('SUM(fee)');
        } else {
            $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['type' => 1, 'daili_id' => $this->id])->whereBetween('created_at', [$begin, $end])->value('SUM(fee)');
        }
        return $fee ? $fee : 0;
    }

    /**
     * [totalCash 总佣金]
     * @return [type] [description]
     */
    public function totalCash()
    {
        if ($this->level == 1) {
            $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['type' => 2, 'second_id' => $this->id])->value('SUM(fee)');
        } elseif ($this->level == 2) {
            $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['type' => 3, 'third_id' => $this->id])->value('SUM(fee)');
        } elseif ($this->level == 3) {
            $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['type' => 4, 'fourth_id' => $this->id])->value('SUM(fee)');
        }
        return $fee ? $fee : 0;
    }

    /**
     * [totalWithdraw 累计提现金额]
     * @return [type] [description]
     */
    public function totalWithdraw()
    {
        $fee = Withdraw::select(DB::raw('SUM(money)'))->where(['agent_id' => $this->id])->whereIn('status', [0, 1])->value('SUM(money)');
        return $fee ? $fee : 0;
    }

    /**
     * [forMyCash 代理为我返利]
     * @return [type] [description]
     */
    public function agentForMyCash($obj)
    {
        if ($this->level == 2) {
            if ($obj->level == 3) {
                $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['third_id' => $this->id, 'fourth_id' => $obj->id, 'type' => 3])->value('SUM(fee)');
            }
        } elseif ($this->level == 1) {
            if ($obj->level == 3) {
                $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['second_id' => $this->id, 'fourth_id' => $obj->id, 'type' => 2])->value('SUM(fee)');
            } elseif ($obj->level == 2) {
                $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['second_id' => $this->id, 'third_id' => $obj->id, 'type' => 2])->value('SUM(fee)');
            }
        }
        return $fee ? $fee : 0;
    }

    /**
     * [userForMyCash 玩家为我返利]
     * @return [type] [description]
     */
    public function userForMyCash($obj)
    {
        if ($this->level == 1) {
            $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['second_id' => $this->id, 'user_id' => $obj->userid, 'type' => 2])->value('SUM(fee)');
        } elseif ($this->level == 2) {
            $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['third_id' => $this->id, 'user_id' => $obj->userid, 'type' => 3])->value('SUM(fee)');
        } elseif ($this->level == 3) {
            $fee = RebateLog::select(DB::raw('SUM(fee)'))->where(['fourth_id' => $this->id, 'user_id' => $obj->userid, 'type' => 4])->value('SUM(fee)');
        }
        
        return $fee ? $fee : 0;
    }

    /**
     * [hasClub 判断代理是否创建俱乐部]
     * @return boolean [description]
     */
    public function hasClub()
    {
        $user = $this->user();
        if ($user) {
            $club = Club::where(['creator' => $user->userid])->whereIn('type', [2,3,4,5,6])->first();
            if ($club) {
                return true;
            }   
        }
        return false;
    }

    public function superagent()
    {
        return $this->hasOne('\App\SuperAgent', 'id', 'superagent_id');
    }
}    